package com.hzt.agprosaleplatform.controller;

import com.hzt.agprosaleplatform.entity.FinanceApplication;
import com.hzt.agprosaleplatform.service.FinanceApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/apis/finance")
public class FinanceApplicationController {

    @Autowired
    private FinanceApplicationService service;

    @PostMapping("/apply")
    public Map<String, Object> apply(@RequestBody FinanceApplication application) {
       Map<String, Object> result = new HashMap<>();
       try {
            FinanceApplication saved = service.createApplication(application);
            result.put("success", true);
           result.put("data", saved);
        } catch (Exception e) {
            result.put("success", false);
           result.put("message", e.getMessage());
        }
        return result;
    }

    @GetMapping("/{financeId}")
    public Map<String, Object> getById(@PathVariable Integer financeId) {
        Map<String, Object> result = new HashMap<>();
        try {
            FinanceApplication app = service.getApplicationById(financeId);
            result.put("success", true);
            result.put("data", app);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    @GetMapping("/user/{userId}")
    public Map<String, Object> getByUserId(@PathVariable String userId) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<FinanceApplication> list = service.getApplicationsByUserId(userId);
            result.put("success", true);
            result.put("data", list);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    @GetMapping("/status/{status}")
    public Map<String, Object> getByStatus(@PathVariable Integer status) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<FinanceApplication> list = service.getApplicationsByStatus(status);
            result.put("success", true);
            result.put("data", list);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    @PutMapping("/{financeId}/status")
    public Map<String, Object> updateStatus(@PathVariable Integer financeId,
                                            @RequestParam Integer status,
                                            @RequestParam String opinion) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = service.updateApplicationStatus(financeId, status, opinion);
            result.put("success", success);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    @PutMapping("/{financeId}/approve")
    public Map<String, Object> approve(@PathVariable Integer financeId, @RequestParam String opinion) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = service.approveApplication(financeId, opinion);
            result.put("success", success);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    @PutMapping("/{financeId}/reject")
    public Map<String, Object> reject(@PathVariable Integer financeId, @RequestParam String opinion) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = service.rejectApplication(financeId, opinion);
            result.put("success", success);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
        }
        return result;
    }

    // 新增：取消融资申请接口（POST或PUT均可，推荐POST）
    @PostMapping("/{financeId}/cancel")
    public Map<String, Object> cancel(@PathVariable Integer financeId, @RequestBody Map<String, String> params) {
        Map<String, Object> result = new HashMap<>();
        try {
            // 获取取消原因（前端传入，可为空）
            String cancelReason = params.getOrDefault("cancelReason", "无");
            boolean success = service.cancelApplication(financeId, cancelReason);
            result.put("success", success);
            result.put("message", success ? "取消成功" : "取消失败");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());  // 返回业务异常信息
        }
        return result;
    }
}
